﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

//using Raize.CodeSiteLogging;

namespace Pub
{
    public class CConfig
    {

        public static string configFile = "";


        #region Methods

        public static string ConnectionString(string key)
        {
            string _connectionString = ConfigurationManager.AppSettings[key];
            string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (ConStringEncrypt == "true")
            {
                //_connectionString = DESEncrypt.Decrypt(_connectionString);
            }
            return _connectionString;
        }

        public static string getConStr(int i_index)
        {
            return ConfigurationManager.ConnectionStrings[i_index].ConnectionString;
        }

        public static string getPermite(string key)
        {
            string emp = ConfigurationManager.AppSettings[key];
            return emp;
        }

        public static string ReadConfigVal(string sKey)
        {
            try
            {
                if (configFile == "") getConfigFileName();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configFile);
                XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='" + sKey + "']");
                return xmlNode.Attributes["value"].InnerText;
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                return null;
            }
        }

        public static void SaveConfigVal(string sKey, string sVal)
        {
            try
            {
                if (sKey == "") return;

                if (configFile == "") getConfigFileName();

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configFile);
                XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='" + sKey + "']");
                xmlNode.Attributes["value"].InnerText = sVal;
                xmlDoc.Save(configFile);
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }
        }


        public static void getConfigFileName()
        {
            configFile = Application.ExecutablePath + ".config";
        }
        #endregion Methods
    }
}